package com.iflytek.cuckoo.controller;

import com.iflytek.cuckoo.dao.UserRepository;
import com.iflytek.cuckoo.entity.User;
import com.iflytek.cuckoo.service.CacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p><一句话功能简述>
 * <p><功能详细描述>
 * <p>
 * <p>Copyright (c) 2018, liangzhang14@iflytek.com All Rights Reserve</p>
 * <p>Company : 科大讯飞</p>
 *
 * @author liangzhang14
 * @version [V1.0, 2019年01月29日 15:16]
 * @see [相关类/方法]
 */
@Slf4j
@RestController
public class HelloCuckooController {

    @Autowired
    UserRepository userRepository;

    @Autowired
    private CacheService cacheService;

    @Autowired
    @Qualifier("userRedisTemplate")
    private RedisTemplate<String,User> rts;

    @RequestMapping("helloCuckoo")
    public String helloCuckoo(HttpServletRequest request) {
        log.info("helloCuckoo {}",new Date());

        return "Hello Cuckoo localPort:" +request.getLocalPort() + " remotePort:" + request.getRemotePort();
    }

    @RequestMapping(value = "hello/{path}", method = RequestMethod.GET)
    public List<User> toHello1(@PathVariable("path") String path){
        log.info("hello {}",path);
        List<User> users = userRepository.findAll();
        System.out.println(path);
        return users;
    }

    @RequestMapping("toHello")
    public List<User> toHello(ModelMap modelMap){
        log.info("toHello {}",modelMap);
        User user = new User();
        user.setName("Mshu");
        user.setPassword("123456");
        user.setEmail("zhuiqiu95@foxmail.com");
        userRepository.save(user);
//        rts.opsForValue().set("user",user);
//        cacheService.set();
        List<User> users = userRepository.findAll();
        modelMap.put("users",users);
        return users;
    }

    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable Long id) {
        log.info("user {}",id);
        System.out.println(new Date());
        System.out.println(id);
        User user = userRepository.findUserById(id);
        return user;
    }

    @GetMapping("/users")
    public List<User> users(){
        log.info("users {}","jj");
        List<User> users = userRepository.findAll();
        return users;
    }
    @GetMapping("/user/search/{searchKey}")
    public List<User> getUsersBySearchKey(@PathVariable String searchKey) {
        log.info("user/search {}",searchKey);
        List<User> users = userRepository.findByNameContains(searchKey);
        return users;
    }

    @RequestMapping(value = "/echo", method = RequestMethod.GET)
    public String echo(@RequestParam String msg) {
        return "【ECHO】" + msg;
    }

    @GetMapping("/user/search/{name}/{email}")
    public List<User> getUsersBySearchKey(@PathVariable String name, @PathVariable String email) {
        List<User> users = userRepository.findByAppNameAndEmail(name, email);
        return users;
    }
}
